package com.gitee.secretopen.sso.controller;

import cn.chenc.framework.core.annotation.BussinessLog;
import cn.chenc.framework.core.model.response.ResponseVO;
import cn.chenc.framework.core.model.response.ResultUtil;
import com.gitee.secretopen.sso.controller.form.LoginForm;
import com.gitee.secretopen.sso.controller.form.ssoLoginForm;
import com.gitee.secretopen.sso.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @Author Xjt
 * @Date 2021/2/26 11:34
 * @Version 1.0
 */
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping("/sso/getUser")
    public ResponseVO getUserByToken(@RequestBody Map<String, String> tokens) {
        String token = tokens.get("token");
        Object user = loginService.getUserByToken(token);
        return ResultUtil.success("获取成功！",user);
    }

    @PostMapping("/sso/login")
    public ResponseVO ssoLogin(@Valid @RequestBody ssoLoginForm ssoLoginForm){
        Map<String, Object> user = loginService.ssoLogin(ssoLoginForm.getName(), ssoLoginForm.getPassword(),ssoLoginForm.getCode(), ssoLoginForm.getSiteId());
        return ResultUtil.success("登录成功！",user);
    }

    @PostMapping("/admin/login")
    @BussinessLog("后台登录")
    public ResponseVO adminLogin(@Valid @RequestBody LoginForm loginForm){
        Map<String, Object> user = loginService.adminLogin(loginForm.getName(), loginForm.getPassword(), loginForm.getCode());
        return ResultUtil.success("登录成功！",user);
    }

    @PostMapping("/admin/logout")
    public ResponseVO adminLogout(@RequestBody Map<String, String> tokens) {
        String token = tokens.get("token");
        if (loginService.adminLogout(token)) {
            return ResultUtil.success("登出成功！");
        }
        return ResultUtil.success("登出失败，请重新登录！");
    }

    @PostMapping("/sso/logout")
    public ResponseVO ssoLogout(@RequestBody Map<String, String> tokens) {
        String token = tokens.get("token");
        if (loginService.ssoLogout(token)) {
            return ResultUtil.success("登出成功！");
        }
        return ResultUtil.success("登出失败，请重新登录！");
    }
}
